/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.alibaba.excel.EasyExcel;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.MatchData;
import me.zhengjie.modules.system.excel.listener.MatchDataListener;
import me.zhengjie.modules.system.excel.vo.MatchDataVo;
import me.zhengjie.modules.system.excel.vo.WarningMessageVo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.MatchDataRepository;
import me.zhengjie.modules.system.service.MatchDataService;
import me.zhengjie.modules.system.service.dto.MatchDataDto;
import me.zhengjie.modules.system.service.dto.MatchDataQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.MatchDataMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;
import org.springframework.web.multipart.MultipartFile;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author 李忠臻
* @date 2024-03-01
**/
@Service
@RequiredArgsConstructor
public class MatchDataServiceImpl implements MatchDataService {

    private final MatchDataRepository matchDataRepository;
    private final MatchDataMapper matchDataMapper;

    private final MatchDataListener matchDataListener;
    @Override
    public PageResult<MatchDataDto> queryAll(MatchDataQueryCriteria criteria, Pageable pageable){
        Page<MatchData> page = matchDataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(matchDataMapper::toDto));
    }

    @Override
    public List<MatchDataDto> queryAll(MatchDataQueryCriteria criteria){
        return matchDataMapper.toDto(matchDataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public MatchDataDto findById(Long id) {
        MatchData matchData = matchDataRepository.findById(id).orElseGet(MatchData::new);
        ValidationUtil.isNull(matchData.getId(),"MatchData","id",id);
        return matchDataMapper.toDto(matchData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(MatchData resources) {
        matchDataRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MatchData resources) {
        MatchData matchData = matchDataRepository.findById(resources.getId()).orElseGet(MatchData::new);
        ValidationUtil.isNull( matchData.getId(),"MatchData","id",resources.getId());
        matchData.copy(resources);
        matchDataRepository.save(matchData);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            matchDataRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<MatchDataDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MatchDataDto matchData : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("条码", matchData.getModel());
//            map.put("条码", matchData.getBarCode());
            map.put("品牌", matchData.getBrand());
            map.put("商品编码", matchData.getModel());
            map.put("商品名称", matchData.getProductName());
            map.put("单位", matchData.getUnit());
            map.put("重量", matchData.getWeight());
            map.put("数量", matchData.getQuantity());
//            map.put("创建者", matchData.getCreateBy());
//            map.put("更新者", matchData.getUpdateBy());
//            map.put("创建日期", matchData.getCreateTime());
//            map.put("更新时间", matchData.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void upload(String name, MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), MatchDataVo.class, matchDataListener).sheet(0).doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new BadRequestException(e.getMessage());
        }
    }
}